package com.st.emps.staff.service;

import java.util.List;

import com.st.emps.department.model.DepartmentModel;
import com.st.emps.staff.dao.StaffDao;
import com.st.emps.staff.model.StaffModel;
import com.st.emps.util.MD5;

public class StaffService {

	private StaffDao dao = new StaffDao();
	private String password = MD5.encode("123456");
	
	public String insert(StaffModel model) {
		if(dao.select(new StaffModel(model.getCode()))== null||dao.select(new StaffModel(model.getCode())).isEmpty()) {
			model.setPassword(password);
			return dao.insert(model)+"";
		}else
		return "repeat";

	}
	public String delete(StaffModel model) {
		return dao.delete(model)+"";

	}
	public String update(StaffModel model) {
		return dao.update(model)+"";

	}
	public List<StaffModel> selectList(StaffModel model) {
		String code = model.getCode();
		if(code==null)
			code ="";
		model.setCode("%"+code+"%");
		String name = model.getName();
		if(name==null)
			name ="";
		model.setName("%"+name+"%");
		return dao.select(model);

	}
	public StaffModel selectModel(StaffModel model) {
		StaffModel tmp = new StaffModel(model.getCode());
		List<StaffModel> list = selectList(tmp);
		return list==null||list.isEmpty()?null:list.get(0);

	}
	public String login(StaffModel model) {
		StaffModel tmp = new StaffModel(model.getCode());
		List<StaffModel> list = dao.select(tmp);
		if(list==null||list.isEmpty()) {
			return "0";
		}
		StaffModel tmp2 = list.get(0);
		if(tmp2.getPassword().equals(MD5.encode(model.getPassword()))) {
			return "1";
		}else {
			return "2";
		}
	}
	public Object selectCount(StaffModel model) {
		StaffModel tmp = new StaffModel();
		String code = model.getCode();
		if(code==null)
			code ="";
		tmp.setCode("%"+code+"%");
		String name = model.getName();
		if(name==null)
			name ="";
		tmp.setName("%"+name+"%");
		return dao.selectCount(tmp);
	}

	public String resetPassword(StaffModel model) {
        model.setPassword(password);
		return dao.updatePassword(model)+"";
	}
	public String updatePass(StaffModel model, String password2) {
		 String password = selectModel(model).getPassword();
		if(password.equals(MD5.encode(model.getPassword()))) {
			model.setPassword(MD5.encode(password2));
			String s = dao.updatePassword(model)+"";
			return s;
		}else {
			return "repeat";
		}
		
	}
	public String updateDepart(StaffModel model) {
		return dao.updateDepart(model)+"";
	}
	
	public String updateImage(StaffModel model) {
		return dao.updateImage(model)+"";
	}
}
